//
//  WBMainViewController.swift
//  WeiboSeven
//
//  Created by 刘刘 on 16/11/23.
//  Copyright © 2016年 刘刘. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {
    
    /// 设置按钮的懒加载属性
    lazy var composeButton: UIButton = UIButton(title: nil, image: "tabbar_compose_icon_add", bgImage: "tabbar_compose_button")
        
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        setupUI()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubview(toFront: composeButton)
    }
}

// MARK: - 设置UI
extension WBMainViewController {
    /// 设置UI
    func setupUI()  {
        // 添加所有子控制器
        addChildViewControolers()
        // 添加一个composeButton
        addComposeButton()
    }
    
    /// 添加所有子控制器
    func addChildViewControolers() {
        //处理异常
        // try?: 弱try, 最终会返回一个可选值,如果有异常会返回nil
        // try!: 强try, 如果抛出异常会崩溃: 不推荐使用
        // do {} catch, 可以打印出异常(error)的信息
        /* 实现思路:
         1. 从main.json读取子控制器的参数data
         2. 将jsonData反序列化成参数字典数组
         3. 使用数组字典参数,初始化子控制器.
         */
        if let url = Bundle.main.url(forResource: "main.json", withExtension: nil) {
            if let data = try? Data(contentsOf: url) {
                // 解析json
                // 在swift中,所有options的参数,都需要传数组
                if let controllersDicArray = try? JSONSerialization.jsonObject(with: data, options: []) as! [[String: Any]] {
                    // 遍历字典数组,创建子控制器
                    for dict in controllersDicArray {
                        
                        if let className = dict["clsName"] as? String,
                            // 用字符串创建一个类
                            let controllerClass = NSClassFromString(Bundle.main.nameSpace + "." + className) as? WBRootViewController.Type {
                            
                            // tabbar的title的image
                            let title = dict["title"] as? String
                            let image = dict["imageName"] as? String
                            
                            // 用类创建一个对象
                            let controller = controllerClass.init()
                            
                            // 如果函数的参数可以传nil,那么该参数就因该设成可选值
                            addChildViewController(childController: controller, title: title, image: image)
                        }
                    }
                }
            }
        }
    }
    
    /// 往tabbar添加一个子控制器
    ///
    /// - Parameters:
    ///   - childController: 子控制器
    ///   - title: 子控制器的title
    ///   - image: tabbaritem的图片名字
    func addChildViewController(childController: UIViewController, title: String?, image: String?) {
        // 文字
        childController.title = title
        //设置tabbaritem的文字的大小和颜色
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 11), NSForegroundColorAttributeName: gloablColor], for: .selected)
         childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 11), NSForegroundColorAttributeName: UIColor.darkGray], for: .normal)
        // 图片
        if let image = image {
            childController.tabBarItem.image = UIImage(named: "tabbar_\(image)")?.withRenderingMode(.alwaysOriginal)
            childController.tabBarItem.selectedImage = UIImage(named: "tabbar_\(image)_selected")?.withRenderingMode(.alwaysOriginal)
        }
        
        let navController = UINavigationController(rootViewController: childController)
        addChildViewController(navController)
    }
    
    /// 添加发布按钮
    func addComposeButton () {
        tabBar.addSubview(composeButton)
        composeButton.sizeToFit()
        composeButton.center = CGPoint(x: tabBar.center.x, y: tabBar.bounds.size.height/2)
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    }
}

// MARK: - 添加点击事件
extension WBMainViewController {
    func composeStatus() {
        print("hello, world")
    }
}

